package com.rentcars.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rentcars.entity.Cars;
import com.rentcars.entity.Cliencompany;
import com.rentcars.entity.Clientuser;
import com.rentcars.entity.Dept;
import com.rentcars.service.RentalcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 罗孝天
 */
@RestController
public class RentalcarController {

    @Autowired
    private RentalcarService rentalcarService;

    @RequestMapping("/addRentalCar")
    public void addRentalInfor(Clientuser user, Cliencompany company,String uid) {
        rentalcarService.insertRentalCar(user, company,uid);

    }

    @RequestMapping(value ="/getDept",method = RequestMethod.GET)
    public Map<String,List<Dept>> getDeptList(){
        List<Dept> deptList = rentalcarService.searchDept();
        Map<String,List<Dept>> rsMap = new HashMap<>();
        rsMap.put("deptList", deptList);
        return rsMap;
    }

    @RequestMapping(value ="/getCar",method = RequestMethod.GET)
    public Map<String,Cars> getCars(String carId,String getDept,String backDept){
        Map<String,Cars> getMap=new HashMap<>();
        String getDeptAddress=rentalcarService.getDeptAddress(getDept);
        String backDeptAddress=rentalcarService.getDeptAddress(backDept);
        Cars car=rentalcarService.getCarInfo(carId);
//        car.setGetdeptAddress(getDeptAddress);
//        car.setBackdeptAddress(backDeptAddress);
//        car.setTotalprice(car.getDayprice()+car.getBasicinsure());
        getMap.put("car",car);
        return getMap;
    }

    @RequestMapping( value = "/goToOrder",method = RequestMethod.GET)
    public Map<String, Object> toOrder(@RequestParam int draw, @RequestParam int start, @RequestParam int length){
        int pageNum = (start/length)+1;//获取当前查询第几页
        int pageSize = length;
        PageHelper.startPage(pageNum, pageSize);
        List carList=rentalcarService.searchCars();
        PageInfo<Cars> page=new PageInfo<Cars>(carList);

        Map<String,Object> rsMap = new HashMap<>();
        rsMap.put("draw", draw);
        rsMap.put("data", page.getList());
        rsMap.put("recordsTotal", page.getTotal());
        rsMap.put("recordsFiltered", page.getTotal());
        return rsMap;
    }

    @RequestMapping("/rental_car")
    public ModelAndView toRental(){
        return new ModelAndView("rental_car");
    }



}
